package com.foodplus.www.adapter.shop;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.foodplus.www.bean.shop.QueryBean;
import com.foodplus.www.foodplus.R;
import com.foodplus.www.view.activity.GoodActivity;

import java.util.List;

/**
 * Created by My on 2016/10/31.
 */

public class ShopAdapter extends RecyclerView.Adapter implements View.OnClickListener{

    private List<QueryBean> list;
    private Context context;
    private OnItemClickListener onItemClickListener;

    public ShopAdapter(List<QueryBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_shop_item,parent,false);
        ShopItemViewHolder holder = new ShopItemViewHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {

        ((ShopItemViewHolder)holder).title.setText(list.get(position).getSpecial_title());

        Glide.with(context)
                .load(list.get(position).getSpecial_image())
                .placeholder(R.mipmap.bg_morentu)
                .fitCenter()
                .into(((ShopItemViewHolder)holder).iv);

    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public void onClick(View v) {

    }


    class ShopItemViewHolder extends RecyclerView.ViewHolder{

        public LinearLayout linearLayout;
        public TextView title;
        public ImageView iv;

        public ShopItemViewHolder(View itemView) {
            super(itemView);

            linearLayout = (LinearLayout) itemView.findViewById(R.id.shop_item_main);
            title = (TextView) itemView.findViewById(R.id.shop_item_title);
            iv = (ImageView) itemView.findViewById(R.id.shop_item_iv);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClickListener(getLayoutPosition());
                    }
                }
            });


        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }



    public interface OnItemClickListener {
        void onItemClickListener(int position);
    }
}
